package org.xiyu.yee.eggpreview.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.xiyu.yee.eggpreview.client.SpawnEggPreviewRenderer;

@Mixin(GuiGraphics.class)
public abstract class MixinGuiGraphics {

    @Shadow public abstract PoseStack pose();

    @Inject(method = "renderItem(Lnet/minecraft/world/item/ItemStack;II)V",
            at = @At("HEAD"), cancellable = true)
    private void eggpreview$renderSpawnEgg(ItemStack stack, int x, int y, CallbackInfo ci) {
        if (stack != null && SpawnEggPreviewRenderer.canRender(stack)) {
            MultiBufferSource.BufferSource buffer = Minecraft.getInstance().renderBuffers().bufferSource();
            boolean ok = SpawnEggPreviewRenderer.renderPreview(this.pose(), x, y, stack, buffer, 0x00F000F0);
            if (ok) {
                buffer.endBatch();
                ci.cancel();
            }
        }
    }

    @Inject(method = "renderItem(Lnet/minecraft/world/item/ItemStack;III)V",
            at = @At("HEAD"), cancellable = true, remap = true)
    private void eggpreview$renderSpawnEggSeed(ItemStack stack, int x, int y, int seed, CallbackInfo ci) {
        if (stack != null && SpawnEggPreviewRenderer.canRender(stack)) {
            MultiBufferSource.BufferSource buffer = Minecraft.getInstance().renderBuffers().bufferSource();
            boolean ok = SpawnEggPreviewRenderer.renderPreview(this.pose(), x, y, stack, buffer, 0x00F000F0);
            if (ok) {
                buffer.endBatch();
                ci.cancel();
            }
        }
    }
}
